package cn.tx.coolshark.controller;

import cn.tx.coolshark.mapper.ProductMapper;
import cn.tx.coolshark.pojo.dto.ProductDto;
import cn.tx.coolshark.pojo.dto.ProductUpdateDto;
import cn.tx.coolshark.pojo.entity.Product;
import cn.tx.coolshark.pojo.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/product/")
public class ProductController {
    @Autowired
    private ProductMapper productMapper;

    @RequestMapping("insert")
    public void insert(@RequestBody ProductDto productDto){
        System.out.println("productDto = " + productDto);
        //把dto数据装进entity
        Product product = new Product();
        BeanUtils.copyProperties(productDto,product);
        //还缺创建时间created和浏览量viewCount
        product.setCreated(new Date());
        product.setViewCount(0);
        //创建ProductMapper 实现insert方法
        productMapper.insert(product);
    }

    @RequestMapping("select")
    public List<ProductAdminVO> select(){
       return productMapper.selectForAdmin();
    }
    @RequestMapping("delete")
    public void delete(int id){
        //先删除商品对应的商品图片
        String url = productMapper.selectUrlById(id);
        new File("d:/upload"+url).delete();
        //然后删除数据库中的商品数据
        productMapper.deleteById(id);
    }

    @RequestMapping("selectById")
    public ProductUpdateVO selectById(int id){
        return productMapper.selectById(id);
    }
    @RequestMapping("update")
    public void update(@RequestBody ProductUpdateDto productUpdateDto){
        System.out.println("productUpdateDto = " + productUpdateDto);
        //得到原来图片的路径
        String url = productMapper.selectUrlById(productUpdateDto.getId());
        //判断原来的路径和新的路径是否一致
        if(!url.equals(productUpdateDto.getUrl())){
            new File("d:/upload"+url).delete();
        }
        //增和改都需要将dto转成entity
        Product product = new Product();
        BeanUtils.copyProperties(productUpdateDto,product);
        //修改商品数据
        productMapper.update(product);
    }

    @RequestMapping("top")
    public List<ProductTopVO> top(){
        return productMapper.selectTop();
    }

    @RequestMapping("index")
    public List<ProductIndexVO> selectIndex(){
        return productMapper.selectIndex();
    }

    @RequestMapping("detail")
    public ProductDetailVO detail(int id, HttpSession session){
        //增加浏览量之前，先去session中查，是否有这个view+id
        String info = (String) session.getAttribute("view"+id);
        if(info==null){//代表没有浏览过
            //让当前商品的浏览量+1
            productMapper.updateViewCountById(id);
            //浏览量已经加1，把view+id存入session
            session.setAttribute("view"+id,"visited");
        }
        return productMapper.selectDetailById(id);
    }

    @RequestMapping("category")
    public List<ProductIndexVO> category(int id){
        return productMapper.selectByCategoryId(id);
    }

    @RequestMapping("search")
    public List<ProductIndexVO> search(String wd){
        return productMapper.selectByWd(wd);
    }
}

